package com.zlyx.easy.web.generate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zlyx.easy.core.list.Lists;
import com.zlyx.easy.swagger.annotations.SpringController;
import com.zlyx.easy.swagger.annotations.SpringMapping;
import com.zlyx.easy.web.generate.model.ModuleInfo;

/**
 * @Auth 赵光
 * @Desc 描述
 * @Date 2019年1月19日
 */
@SpringController(value = { "/code" }, todo = { "代码生成" }, level = -1)
public class GenerateController {

	@Autowired
	private EasyGenerate easyGenerate;

	@SpringMapping(value = { "/excute" }, todo = { "执行代码生成" })
	public String excute(@RequestBody String moduleInfo) {
		JSONObject jsonObject = JSON.parseObject(moduleInfo);
		ModuleInfo info = new ModuleInfo();
		info.setAuth(jsonObject.getString("module_auth"));
		info.setPath(jsonObject.getString("module_path"));
		JSONArray entity_names = jsonObject.getJSONArray("entity_names");
		JSONArray entity_paths = jsonObject.getJSONArray("entity_paths");
		JSONArray entity_tables = jsonObject.getJSONArray("entity_tables");
		info.setNames(Lists.newList(entity_names.toJavaList(String.class).subList(0, entity_names.size())));
		info.setPaths(Lists.newList(entity_paths.toJavaList(String.class).subList(0, entity_paths.size())));
		info.setTables(Lists.newList(entity_tables.toJavaList(String.class).subList(0, entity_tables.size())));
		easyGenerate.excute(info);
		return "success";
	}
}
